// pages/shop/shop-collect/shop-collect.js
var httpUtil = require('../../../utils/httpUtil.js');
var uiUtil = require('../../../utils/uiUtil.js');
const app = getApp();
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLocation: null,//用户地理位置
    mchLists:{
      totalCount: null,
      pageCount: null,
      pageNo: "0",
      pageSize: "30",
      dataList: []
    },
    requestGoodsTask: null, //请求task
    isLoadingMore: false,
    enableLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.setData({
      userLocation: app.globalData.userLocation
    });
    that.myCollectLists();
  },

  /**
   * 我的收藏查询
   */
  myCollectLists() {
    let pageNo = parseInt(that.data.mchLists.pageNo)+1;
    let pageSize = that.data.mchLists.pageSize;
    let dataList = that.data.mchLists.dataList;
    let lat = that.data.userLocation.pois[0].location.lat;
    let lng = that.data.userLocation.pois[0].location.lng;

    var lastRequestTask = that.data.requestGoodsTask;
    if (lastRequestTask) {
      try {
        lastRequestTask.abort()
      } catch (e) { }
    }

    var curPage = that.data.mchLists.pageNo;
    if (curPage != 0 && (!that.data.enableLoadMore || that.data.isLoadingMore)) { //不可加载||正在加载\
      return;
    }

    var requestTask =httpUtil.PostRequest("searchCollect", {
      pageNo: pageNo,
      pageSize: pageSize,
      lat: lat,
      lng: lng
    }, {
      success(res) {
        if (0 == that.data.mchLists.pageNo) {
          res.dataList = res.dataList || []
        } else {
          res.dataList = dataList.concat(res.dataList || []);
        }
        that.setData({
          mchLists: res,
          touchThat: null,
          enableLoadMore: res.dataList.length < res.totalCount,
          fail: false
        })
      },
      fail(code, res) {
        that.setData({
          enableLoadMore: false
        })
        if (0 == that.data.mchLists.pageNo) { //内容为空
          that.setData({
            fail: true
          })
        } else {
          uiUtil.showToast(res);
        }
      },
      complete() {
        that.setData({
          isLoadingMore: false
        })
      }
    })
    that.setData({
      requestGoodsTask: requestTask
    })
  },

  /**
   * 取消收藏
   */
  deleteCollect:function(e){
    wx.showModal({
      title: '取消收藏',
      content: '确定要取消收藏该商家吗?',
      showCancel: true,
      success(res) {
        if (res.confirm){
          let id=e.currentTarget.dataset.id;
          httpUtil.PostRequest("delCollect", {
            id:id
          }, {
              success(res) {
                that.onResultRegisterSuccess();
              },
              fail(code, res) {
                uiUtil.showToast(res);
              }
            })
        }
      }
    })
  },

  /**
  * 触底加载
  */
  onReachBottom(){
    that.myCollectLists();
  },


  /**
  * 回调
  */
  onResultRegisterSuccess() {
    that.setData({
      mchLists: {
        totalCount: null,
        pageCount: null,
        pageNo: "0",
        pageSize: "30",
        dataList: []
      },
      requestGoodsTask: null, //请求task
      isLoadingMore: false,
      enableLoadMore: true
    })
    that.myCollectLists();
  },

  /**
   * 左滑删除
   */
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
          //设置触摸起始点水平方向位置
          startX: e.touches[0].clientX
      });
    }
  },

  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = this.data.startX - endX;
      // //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      if(disX>50){
        that.setData({
          touchThat: index
        });
      } else if(disX<-50){
        that.setData({
          touchThat: null
        });
      }
    }
  },
})